package com.atguigu.vod.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.Utils.ConstanPropertiesVodUtils;
import com.atguigu.vod.Utils.aliyunVodUtils.InitObject;
import com.atguigu.vod.Utils.aliyunVodUtils.PutUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class VodServiceImpl implements VodService {


    @Override
    public String uploadAlyiVideo(MultipartFile file) {
        String videoId = PutUploadUtils.aliyunPutUpload(file);
        if (videoId == null) {
            throw new GuliException(20001,"上传视频失败");
        }
        return videoId;
    }

    @Override
    public void removeAlyVideo(String videoId) throws ClientException {
        //初始u啊
        DefaultAcsClient defaultAcsClient = InitObject.initVodClient(ConstanPropertiesVodUtils.KEY_ID, ConstanPropertiesVodUtils.KEY_SECRET);
        //创建删除对象
        DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
        //传入删除id
        deleteVideoRequest.setVideoIds(videoId);
        //进行删除操作
        defaultAcsClient.getAcsResponse(deleteVideoRequest);
    }

    @Override
    public void deleteBatch(List<String> videoIdList) throws ClientException {
        //初始u啊
        DefaultAcsClient defaultAcsClient = InitObject.initVodClient(ConstanPropertiesVodUtils.KEY_ID, ConstanPropertiesVodUtils.KEY_SECRET);
        //创建删除对象
        DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
        //将list中的id值1,2,3
        String join = StringUtils.join(videoIdList.toArray(), ",");
        //传入删除id列表
        deleteVideoRequest.setVideoIds(join);
        //进行删除操作
        defaultAcsClient.getAcsResponse(deleteVideoRequest);
    }


}
